javal连接mysq数据库

1、需要去mysql官网去下载mysql关于java的压缩包。

2、

<span style="font-size:18px;"><span style="font-size:18px;">try { 
Class.forName("com.mysql.jdbc.Driver");     //加载MYSQL JDBC驱动程序    
//Class.forName("org.gjt.mm.mysql.Driver"); 
System.out.println("Success loading Mysql Driver!"); 
} 
catch (Exception e) { 
System.out.print("Error loading Mysql Driver!"); 
e.printStackTrace(); 
} 
try { 
Connection connect = DriverManager.getConnection( 
    "jdbc:mysql://172.18.17.137:3306/Triose","root","debian"); 
     //连接URL为   jdbc:mysql//服务器地址/数据库名  ,后面的2个参数分别是登陆用户名和密码 

System.out.println("Success connect Mysql server!"); 
Statement stmt = connect.createStatement(); 
ResultSet rs = stmt.executeQuery("select * from Commont"); 
                                                        //user 为你表的名称 
while (rs.next()) { 
	System.out.println(rs.getString("ID"));
  System.out.println(rs.getString("Content")); 
  System.out.println(rs.getString("Rank")); 
  System.out.println(rs.getString("Time"));

} 
} 
catch (Exception e) { 
System.out.print("get data error!"); 
e.printStackTrace(); 
}</span></span>
3、通过get传参,来获得php脚本的输出值。通过需要解码。


<span style="font-size:18px;"><span style="font-size:18px;">try {
			URL url = new URL("http://192.168.239.128:10000/test.php?Func=good");			
	        HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
	        urlcon.connect();         //获取连接
	        InputStream is = urlcon.getInputStream();
	        BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
	        StringBuffer bs = new StringBuffer();
	     /*   String l = "";
	        l = new String(l.getBytes("gbk")); */
	        String str=""; 
	        String a="我们";</span>
	      
	    <span style="font-size:18px;">    while((str=buffer.readLine())!=null){
	        	new String(str.getBytes("ISO8859_1"),"utf-8");	        
	        	bs.append(str).append("/n");
	        	}
	        System.out.println(bs.toString());
	          
	           //System.out.println(" content-encode:"+urlcon.getContentEncoding());
	           //System.out.println(" content-length:"+urlcon.getContentLength());
	           //System.out.println(" content-type:"+urlcon.getContentType());
	           //System.out.println(" date:"+urlcon.getDate());
	                

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}</span></span>

4、php脚本代码

<span style="font-size:18px;"><span style="font-size:18px;"><?php
if(isset($_GET['Func'])&&$_GET['Func']=="good"){
	database();
}

function good()
{
echo "fewf";
}
function database_ini()
{
	$dbname = "zhouchenglin";
     
                // 连接数据库需要的参数
    $host = "localhost";
    $user = "zhouchenglin";
    $pwd = "zhouchenglin";
    
    // 接着调用mysql_connect()连接服务器
    $link = mysql_connect($host,$user,$pwd);
    if(!$link) {</span>
    <span style="font-size:18px;"> die("Connect Server Failed: " . mysql_error());
    }
  if(!mysql_select_db($dbname,$link)) {
     die("Select Database Failed: " . mysql_error($link));    
    }
    
    mysql_query("set names utf8");
    
    return $link;
}

function database()
{
	$link =database_ini();
	//$sql = "insert into access_token(access_token) values('aaa')";
	$sql = "select *from Commont";
	$data = mysql_query($sql,$link);

	while($row = mysql_fetch_array($data)){
		echo $row['Rank'];
		echo $row['Content'];		
	}
}
    

?>
</span></span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值